Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
redux-actions
Advanced tools
The redux-actions npm package provides utilities for creating and handling actions in Redux applications. It simplifies the process of defining action creators and reducers, making the code more readable and maintainable.
createAction
The createAction function simplifies the creation of action creators. It takes a single argument, the action type, and returns an action creator function that can be called with a payload.
const { createAction } = require('redux-actions');
const increment = createAction('INCREMENT');
console.log(increment()); // { type: 'INCREMENT' }
console.log(increment(1)); // { type: 'INCREMENT', payload: 1 }
handleAction
The handleAction function allows you to create a reducer for a specific action type. It takes the action creator, a reducer function, and an initial state as arguments.
const { handleAction } = require('redux-actions');
const increment = createAction('INCREMENT');
const reducer = handleAction(increment, (state, action) => state + action.payload, 0);
console.log(reducer(0, increment(1))); // 1
handleActions
The handleActions function allows you to create a reducer that handles multiple action types. It takes an object mapping action creators to reducer functions and an initial state as arguments.
const { handleActions } = require('redux-actions');
const increment = createAction('INCREMENT');
const decrement = createAction('DECREMENT');
const reducer = handleActions({
[increment]: (state, action) => state + action.payload,
[decrement]: (state, action) => state - action.payload
}, 0);
console.log(reducer(0, increment(1))); // 1
console.log(reducer(1, decrement(1))); // 0
Redux Toolkit is the official, recommended way to write Redux logic. It includes utilities to simplify common use cases like store setup, creating reducers, and writing immutable update logic. Compared to redux-actions, Redux Toolkit offers a more comprehensive set of tools and is maintained by the Redux team.
Redux Saga is a library that aims to make application side effects (e.g., asynchronous actions) easier to manage, more efficient to execute, and better at handling failures. While redux-actions focuses on simplifying action creation and reducers, redux-saga is more about handling complex side effects in a Redux application.
Redux Thunk is a middleware that allows you to write action creators that return a function instead of an action. This can be used to delay the dispatch of an action or to dispatch only if a certain condition is met. Unlike redux-actions, which focuses on simplifying action and reducer creation, redux-thunk is more about handling asynchronous actions.
Flux Standard Action utilities for Redux
$ npm install --save redux-actions
or
$ yarn add redux-actions
The npm package provides a CommonJS build for use in Node.js, and with bundlers like Webpack and Browserify. It also includes an ES modules build that works well with Rollup and Webpack2's tree-shaking.
The UMD build exports a global called window.ReduxActions
if you add it to your page via a <script>
tag. We don’t recommend UMD builds for any serious application, as most of the libraries complementary to Redux are only available on npm.
import { createActions, handleActions, combineActions } from 'redux-actions';
const defaultState = { counter: 10 };
const { increment, decrement } = createActions({
INCREMENT: (amount = 1) => ({ amount }),
DECREMENT: (amount = 1) => ({ amount: -amount })
});
const reducer = handleActions(
{
[combineActions(increment, decrement)]: (
state,
{ payload: { amount } }
) => {
return { ...state, counter: state.counter + amount };
}
},
defaultState
);
export default reducer;
See the full API documentation.
FAQs
Flux Standard Action utlities for Redux
The npm package redux-actions receives a total of 292,186 weekly downloads. As such, redux-actions popularity was classified as popular.
We found that redux-actions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.